package com.xinai.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.xinai.util.DBUtil;
import com.xinai.vo.OutLineStocktaking;

/**
 * Title: OutLineStocktakingDao Description: 离线盘点DAO
 *
 * @author LYJ
 *
 */
public class OutLineStocktakingDao extends BaseDao {

    private static final String TABLE = "outLineStocktaking";
    private static final String DetailTable ="OutLineStocktakingDetail";

    public OutLineStocktakingDao(Context context) {
        super(context);
    }

    public List<OutLineStocktaking> getList() {
        return callBack(TYPE_READ, new DaoCallBack<List<OutLineStocktaking>>() {
            @Override
            public List<OutLineStocktaking> invoke(SQLiteDatabase conn) {
                String sql = " select * from outLineStocktaking t order by t.Id desc; ";
                cursor = conn.rawQuery(sql, null);
                if (cursor == null) {
                    return null;
                }
                List<OutLineStocktaking> list = DBUtil.cursor2VOList(cursor, OutLineStocktaking.class);
                return list;
            }
        });
    }
  //查询是否存在单号
    public List<String> getNoList(final  String no1) {
        return callBack(TYPE_READ, new DaoCallBack<List<String>>() {
            @Override
            public List<String> invoke(SQLiteDatabase conn) {
                String sql = " select distinct [No] from outLineStocktaking where [No]=?";
                cursor = conn.rawQuery(sql, new String[]{no1});
                if (cursor == null) {
                    return null;
                }
                List<String> list = new ArrayList<String>();
                while (cursor.moveToNext()) {
                    String no = cursor.getString(cursor.getColumnIndex("No"));
                    list.add(no);
                }
                return list;
            }
        });
    }
// 列表 返回数据 Convert(varchar(10),a.[Date],121)
    public List<Map<String,Object>> find(final String name) {
        return callBack(TYPE_READ, new DaoCallBack<List<Map<String,Object>>>() {
            @Override
            public List<Map<String,Object>> invoke(SQLiteDatabase conn) {
                cursor = conn.rawQuery("select a.id,a.departmentID,b.[no] Code,u.[No] userno,u.userName,a.UsersID,b.departmentName Name,a.[No], strftime('%Y-%m-%d', a.Date) Date,a.Quantity  from OutLineStocktaking a join sysDepartment b on a.departmentID=b.id left join sysUsers u on a.UsersID=u.id where a.[No] like ? ", new String[] {"%"+name+"%"});
                List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
                while (cursor.moveToNext()) {
                    Map<String,Object> map = new LinkedHashMap<>();
                    map.put("No",cursor.getString(cursor.getColumnIndex("No")));
                    map.put("Date",cursor.getString(cursor.getColumnIndex("Date")));
                    map.put("Name",cursor.getString(cursor.getColumnIndex("Name")));
                    map.put("departmentID",cursor.getString(cursor.getColumnIndex("departmentID")));
                    map.put("DepartmentCode",cursor.getString(cursor.getColumnIndex("Code")));
                    map.put("Quantity",cursor.getString(cursor.getColumnIndex("Quantity")));
                    map.put("userno",cursor.getString(cursor.getColumnIndex("userno")));
                    map.put("UsersID",cursor.getString(cursor.getColumnIndex("UsersID")));
                    map.put("userName",cursor.getString(cursor.getColumnIndex("userName")));

                    list.add(map);
                }
                return list;
            }
        });
    }

    public int update(final OutLineStocktaking OutLineStocktaking) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                ContentValues values = new ContentValues();
                values.put("Quantity", OutLineStocktaking.getQuantity());
                return conn.update(TABLE, values, "[No] = ?", new String[] {String.valueOf(OutLineStocktaking.getNo())});
            }
        });
    }

    public int delete(final String id) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.delete(DetailTable,"relationNo = ? ",new String[]{id});
                return conn.delete(TABLE, " [No] = ? ", new String[] {id});
            }
        });
    }

    public int insert(final OutLineStocktaking OutLineStocktaking) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("insert into OutLineStocktaking([No],departmentID,usersID,Quantity) values(?,?,?,?)", new Object[] {OutLineStocktaking.getNo(), OutLineStocktaking.getDepartmentID(), OutLineStocktaking.getUsersID(),OutLineStocktaking.getQuantity()});
                return 1;
            }
        });
    }

}
